import java.util.Scanner; 
import com.jcraft.jsch.*; 
public class VyattaSSHSnmp extends Thread 
{ 
	static Scanner sc=new Scanner(System.in); 
	static Session session; 
	static Channel channel; 
	public static void main(String[] arg) 
	{ 
        try 
        { 
        	JSch jsch=new JSch();  		
      		LoginDetails ld=new LoginDetails();
       		session=jsch.getSession(ld.getUser(),ld.getHost(), 22); 
    		session.setPassword(ld.getPwd()); 
      		UserInfo ui = new MyUserInfo() 
 	        { 
 	        	public boolean promptYesNo(String message) 
 	        	{   		 
// 	        		System.out.println("enter 0 for exit 1 for continue"); 
 	        		int foo=sc.nextInt(); 
 	        		return foo==1; 
 	        	} 
 	        }; 
 	        session.setUserInfo(ui); 
      		session.connect(30000);   
      	    channel=session.openChannel("shell"); 
      	    channel.connect(3*1000);  	  
      	    VyattaTerminalOutput vteriminal = new VyattaTerminalOutput(channel.getInputStream(),channel.getOutputStream()); 
      	    Thread.sleep(3000);
      	    System.out.println("loginbuffer="+vteriminal.getStrbuff()); 
      	    System.out.println((vteriminal.getStrbuff()).length());
       	    vteriminal.getOutput().write("sudo su -\n".getBytes()); 
       	    vteriminal.getOutput().flush(); 
       	    Thread.sleep(3000);
      	    System.out.println("buffer="+vteriminal.getStrbuff()); 
      	    System.out.println((vteriminal.getStrbuff()).length());
      	    if(vteriminal.getStrbuff().toString().contains("~$")) 
      	    { 
      	    	System.out.println("Successfully entered into Root mode"); 
      	    	vteriminal.getOutput().write("configure\n".getBytes()); 
      	    	vteriminal.getOutput().flush(); 
             	Thread.sleep(2000);
             	if(vteriminal.getStrbuff().toString().contains("~#")) 
             	{
             		System.out.println("Successfully entered into Configure mode"); 
             		vteriminal.getOutput().write("set service snmp community public authorization ro\n".getBytes()); 
          	    	vteriminal.getOutput().flush(); 
                 	Thread.sleep(2000);
	             	System.out.println("loginbuffer="+vteriminal.getStrbuff()); 
	             	vteriminal.getOutput().write("set service snmp community public client 192.168.1.38\n".getBytes()); 
          	    	vteriminal.getOutput().flush(); 
                 	Thread.sleep(2000);
                 	if(vteriminal.getStrbuff().toString().contains("exists"))
                 	{
                 		vteriminal.getOutput().write("delete service snmp\n".getBytes()); 
              	    	vteriminal.getOutput().flush(); 
                     	Thread.sleep(2000);
                 	}
	             	System.out.println("loginbuffer="+vteriminal.getStrbuff());
	            	vteriminal.getOutput().write("commit\n".getBytes()); 
	              	vteriminal.getOutput().flush(); 
	               	Thread.sleep(2000);
	               	System.out.println("loginbuffer="+vteriminal.getStrbuff());	              	
	              	vteriminal.getOutput().write("save\n".getBytes()); 
	            	vteriminal.getOutput().flush(); 
	             	Thread.sleep(2000);
	             	System.out.println("loginbuffer="+vteriminal.getStrbuff());	            	      	  	  
	      	    	vteriminal.getOutput().write("exit\n".getBytes()); 
	            	vteriminal.getOutput().flush(); 
	             	Thread.sleep(2000);
	             	System.out.println("loginbuffer="+vteriminal.getStrbuff());	  
	             	vteriminal.getOutput().flush(); 
             	}
	      	 } 
      	 } 
         catch(Exception e) 
         { 
        	e.printStackTrace(); 
         } 
	} 
	public static abstract class MyUserInfo implements UserInfo, UIKeyboardInteractive 
	{ 
   	   public String getPassword() 
   	   { 
   		   return null; 
   	   } 
       public boolean promptYesNo(String str) 
   	   { 
   		   return false; 
   	   } 
   	   public String getPassphrase() 
   	   { 
   		   return null; 
   	   } 
       public boolean promptPassphrase(String message) 
   	   { 
   		   return false; 
   	   } 
       public boolean promptPassword(String message) 
       { 
   		   return false; 
       } 
       public void showMessage(String message) 
       { 
     	 
       } 
       public String[] promptKeyboardInteractive(String destination,String name,String instruction, String[] prompt,boolean[] echo) 
       { 
               return null; 
       } 
	} 
}
